﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-26 18:15:21
 * 功能：单个图片资源UI
**************************************************************************************/

using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.CityPanel
{
    public class Item : MonoBehaviour
    {
        private GameObject m_Wait;
        private Button m_IconBtn;
        private GameDataModel.ImgData m_Data;
        private GameDataModel.City m_CityData;
        public Item Previous, Next;

        private void Find()
        {
            m_Wait = transform.Find("Wait").gameObject;
            m_IconBtn = transform.Find("Mask/IconBtn").GetComponent<Button>();
            m_IconBtn.onClick.AddListener(OnClickBtn);
        }

        private void OnClickBtn()
        {
            UIUtility.DisplayPanel(CItyPreviewPanel.CItyPreviewPanelView.PANEL_KEY, "UI/CItyPreviewPanel/Prefabs/CItyPreviewPanel", data: this);
        }

        public void Init(GameDataModel.ImgData data, GameDataModel.City cityData)
        {
            if (!gameObject.activeInHierarchy) return;
            m_Data = data;
            m_CityData = cityData;
            if (m_Wait == null) Find();
            m_Wait.SetActive(data == null);
            if (data == null) return;
            m_IconBtn.image.sprite = AssetCacheServer.Instance.GetSprite(data.assetsInfo.type, data.assetsInfo.id);
            var size = UIUtility.CalcSizeToFill(transform as RectTransform, m_IconBtn.image.sprite.texture);
            m_IconBtn.image.rectTransform.sizeDelta = new Vector2(size.width, size.height);
        }

        public void Clear()
        {
            m_Data = null;
            m_CityData = null;
            m_Wait.SetActive(true);
            m_IconBtn.image.sprite = null;
            Previous = null;
            Next = null;
        }

        public Sprite Icon => m_IconBtn.image.sprite;

        public GameDataModel.ImgData Data => m_Data;

        public GameDataModel.City CityData => m_CityData;
    }
}